package com.gy.mbaselibrary.net;

import android.content.Context;

import com.google.gson.Gson;
import com.gy.mbaselibrary.utils.DESUtil;
import com.gy.mbaselibrary.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/1/31
 * 类描述：request基类的建造类
 * 修改人：
 * 修改时间：2018/1/31
 * 修改备注：
 */
public class RequestBuilder {
    //网络请求码
    protected int requestCode;
    ////网络请求回调接口
    protected BaseRequest.ActivityResponseListener activityResponseListener;
    //上下文对象
    protected Context context;
    //tag
    protected Map<String,Object> tagMap;
    //解析类
    protected Class clz;
    //接口地址
    protected String url;
    //联网方式
    protected NetType type;
    //请求类
    protected Map<String,String> para;
    //body
    protected String bodycontent;

    //protected boolean isEncode;
    //header参数
    protected Map<String,String> headMap = new HashMap<>();

    protected RequestBuilder(){
    }

    public static RequestBuilder build(){
        return new RequestBuilder();
    }

    public BaseRequest create(){
        BaseRequest baseRequest = new BaseRequest();
        baseRequest.setRequestCode(requestCode);
        baseRequest.setClass(clz);
        baseRequest.setContext(context);
        baseRequest.setBodycontent(bodycontent);


        if (para != null)
            baseRequest.setParams(para);
        baseRequest.setResponseListener(activityResponseListener);
        baseRequest.setTagMap(tagMap);
        baseRequest.setUrl(url);
        baseRequest.setType(type);
        baseRequest.setHeadMap(headMap);
        return baseRequest;
    }

    public RequestBuilder setRequestCode(int requestCode) {
        this.requestCode = requestCode;
        return this;
    }

    public RequestBuilder setActivityResponseListener(BaseRequest.ActivityResponseListener activityResponseListener) {
        this.activityResponseListener = activityResponseListener;
        return this;
    }



    public RequestBuilder setType(NetType type) {
        this.type = type;
        return this;
    }



    public RequestBuilder setContext(Context context) {
        this.context = context;
        return this;
    }

    public RequestBuilder setTag(Object tag) {
        if (tagMap == null)
            tagMap = new HashMap<>();
        tagMap.put("tag",tag);
        return this;
    }

    public RequestBuilder addTag(String key,Object tag) {
        if ("tag".equals(key)){
            if (context != null){
                ToastUtil.showToast(context,"键已被占用");
            }
            return this;
        }
        if (tagMap == null)
            tagMap = new HashMap<>();
        tagMap.put(key,tag);
        return this;
    }

    public RequestBuilder setClz(Class clz) {
        this.clz = clz;
        return this;
    }

    public RequestBuilder setUrl(String url) {
        this.url = url;
        return this;
    }

    public RequestBuilder setPara(Map<String,String> para) {
        this.para = para;
        return this;
    }

    public RequestBuilder setHeadMap(Map<String, String> headMap) {
        this.headMap = headMap;
        return this;
    }

    public RequestBuilder setBodycontent(String bodycontent) {
        this.bodycontent = bodycontent;
        return this;
    }
}
